<script setup>
import axios from 'axios';
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router'
import { h } from 'vue'
import { ElMessage } from 'element-plus'
import jwt_decode from "jwt-decode";

var phonePatten = /^1[35789]\d{9}$/
var passwordPatten = /^\w{6,12}$/

const codeURL = ref("http://192.168.110.221:8080/user/loginController")

const router = useRouter()
const phone = ref('');
const password = ref("");
const code = ref("");

const isPhoneTrue = ref(true)
const isPasswordTrue = ref(true)

const open = (msg) => {
    ElMessage(msg)
}
const openVn = () => {
    ElMessage({
        message: h('p', null, [
            h('span', null, '消息'),
            h('i', { style: 'color: teal' }, '文字'),
        ]),
    })
}

onMounted(() => {
    let token = localStorage.getItem("token")
    if (token != null) {
        router.push('/')
    } else {
        token = localStorage.getItem("longToken")
        if (token != null) {
            router.push('/')
        }
    }
})

function isPhone() {
    isPhoneTrue.value = phonePatten.test(phone.value)
}
function isPassword() {
    isPasswordTrue.value = passwordPatten.test(password.value)
}
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "null";
}
function getCode() {
    codeURL.value = 'http://192.168.110.221:8080/user/loginController?' + Math.random()
}
function login() {
    let a = getCookie('code')
    if (phone.value == "" || password.value == "") {
        alert("请输入账号和密码")
    } else {
        if (isPhoneTrue.value && isPasswordTrue.value) {
            const userinfo = {
                "loginAccount": phone.value,
                "loginPassword": password.value,
                "verifyCode": code.value
            }
            axios({
                url: "http://192.168.110.221:8080/user/loginController",
                method: "post",
                headers: { 'content-type': 'application/json;charset=utf-8' },
                data: userinfo
            })
                .then((resp) => {
                    if (resp.data.code == 200 && resp.data.datas.status == 1) {
                        localStorage.setItem("token", resp.data.datas.token)
                        localStorage.setItem("longToken", resp.data.datas.longToken)
                        open("登录成功")
                        router.push('/')

                    } else if (resp.data.code == 200 && resp.data.datas.status != 1) {
                        open("用户账号已被冻结,请联系管理员")
                    } else {
                        open(resp.data.msg)
                        code.value = ''
                        getCode()
                    }
                })
                .catch((e) => {
                    console.log(e);
                })
        } else {
            alert('输入有误,请重新输入')
        }
    }
}
</script>

<template>
    <div class="back-box">
        <div style="margin-top: 120px;">
            <h1 class="header">e钱包,e支付</h1>
            <div class="title">
                <!-- <span class="logo">logo</span> -->
                <span>欢迎使用Ewallet</span>
            </div>
            <div class="body">
                <li>
                    <div>
                        <!-- 18067766964 -->
                        <span class="iconfont icon-zhanghao"></span>
                        <input type="text" maxLength="11" v-model="phone" placeholder="请输入账号" @keyup="isPhone()"
                            @keyup.enter="login()">
                    </div>
                    <p v-if="!isPhoneTrue">*请输入正确的手机号</p>
                </li>

                <li>
                    <div>
                        <span class="iconfont icon-mima"></span>
                        <input type="password" v-model="password" placeholder="请输入密码" @keyup="isPassword()"
                            @keyup.enter="login()">
                    </div>
                    <p v-if="!isPasswordTrue">*请输入正确的密码(6-12位数字,字母组成)</p>
                </li>

                <li>
                    <div class="code-div">
                        <span class="iconfont icon-yanzhengyanzhengma"></span>
                        <input type="text" v-model="code" placeholder="请输入验证码" class="code-text" @keyup.enter="login()">
                    </div>
                    <span class="code-pic" @click="getCode"><img :src="codeURL" alt=""></span>
                </li>
                <li>
                    <button class="login-btn" @click="login">登录</button>
                </li>
                <li>
                    <p>登录即代表同意<a href="javascript:void(0)">《用户协议》</a><a href="javascript:void(0)">《隐私协议》</a></p>
                </li>
                <li>
                    <div style="background-color: transparent;border: none;border-radius: 0%;">
                        <RouterLink to="/Register">
                            <p class="forget" style="float: left;">注册</p>
                        </RouterLink>
                        <RouterLink to="/forget-password">
                            <p class="forget">忘记密码</p>
                        </RouterLink>
                    </div>
                </li>
            </div>
        </div>
    </div>
</template>

<style scoped>
* {
    text-align: center;
    list-style: none;
    text-decoration: none;
}

.back-box {
    position: absolute;
    width: 100%;
    height: 100%;
    background: url("../../assets/img/b5.png");
    background-size: 100% 100%;
}

img {
    width: 100%;
    height: 100%;
}

.header {
    font-size: 30px;
    color: rgba(51, 88, 255, 0.781);
    font-style: italic;
}

.title {
    height: 30px;
    line-height: 30px;
    margin-top: 20px;
}

.title span {
    display: inline-block;
    font-family: '宋体';
    font-weight: 400;
    font-size: 15px;
    font-style: italic;
}

.logo {
    width: 30px;
    margin-right: 10px;
}

.body {
    margin-top: 60px;
    overflow: hidden;
}

.body::after {
    display: block;
    content: "";
    clear: both;
}

.iconfont {
    font-family: "iconfont" !important;
    font-size: 16px;
    font-style: normal;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    margin-right: 10px;
}

input {
    text-align: left;
    width: 200px;
    height: 30px;
    outline: none;
    font-size: 16px;
    border: transparent;
    background-color: transparent;
}

.code-text {
    width: 120px;
}

.code-pic {
    display: inline-block;
    height: 32px;
    line-height: 32px;
    margin-left: 8px;
    margin-top: -2px;
    width: 70px;
    border: 1px solid #d6d6d6;
    vertical-align: top;
    background-color: #fff;
}

.body>li {
    margin-top: 20px;
}

.body>li::after {
    margin: 0 auto;
    display: block;
    content: "";
    clear: both;
}

.body>li>div~p {
    width: 260px;
    margin: 0 auto;
    color: red;
    text-align: left;
    text-indent: 10px;
}

.body>li>div {
    overflow: hidden;
    display: inline-block;
    border: 1px solid #d6d6d6;
    width: 260px;
    border-radius: 20px;
    background-color: #fff;
}

.code-div {
    width: 180px !important;
    right: 40px;
}

.login-btn {
    width: 260px;
    height: 30px;
    border-radius: 20px;
    border: transparent;
    font-size: 16px;
    font-weight: 600;
    color: #fff;
    background-color: rgba(11, 86, 247, 0.781);
}

li>p {
    font-size: 13px;
}

.forget {
    font-size: 13px;
    text-align: right;
    color: rgba(11, 86, 247, 0.781);
}

.forget:hover {
    color: rgba(19, 20, 90, 0.705);
}
</style>